package com.example.demo.controller;

import com.example.demo.entity.img;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class imgController {
    @GetMapping("/selectimg/{name}/{user}/{time}")
    private List<img> selectimg(@PathVariable("name") String name, @PathVariable("user") String user, @PathVariable("time") String time) throws IOException {
        time = time.replace(":", ",");
        List<img> imgs = new ArrayList<>();
        if (!new File("C:/miaozishangchengFile/commodity/" + name + "/评价/" + user + "/" + time).exists()) {
        } else {
            File[] listFiles = new File("C:/miaozishangchengFile/commodity/" + name + "/评价/" + user + "/" + time).listFiles();
            for (int i = 0; i < listFiles.length; i++) {
                img img = new img();
                img.setSrc("miaozishangchengFile/commodity/" + name + "/" + "评价/" + user + "/" + time + "/" + (i + 1) + ".jpg");
                BufferedImage sourceImg = ImageIO.read(new FileInputStream(listFiles[i]));
                img.setH(sourceImg.getHeight());
                img.setW(sourceImg.getWidth());
                imgs.add(img);
            }
        }
        return imgs;
    }

}
